package com.pdweb.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pdweb.dao.TurnosDao;
import com.pdweb.vo.Turnos;

/**
 * Servlet implementation class TurnosConsultaServlet
 */
@WebServlet("/TurnosConsultaServlet")
public class TurnosConsultaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TurnosConsultaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doService(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doService(request, response);
		
	}
	
	protected void doService(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		try {

			Turnos turnos= new Turnos();

			turnos.setNomeTurno(request.getParameter("nomeTurno"));
			

			TurnosDao turnosDao = new TurnosDao();
			
			String sql = "select * from Turnos where ST_NOME_TURN = '"+turnos.getNomeTurno()+"';"; 

			List<Turnos> turnos1 = turnosDao.consultaTurno(sql);
			 
			 if( turnos1 != null){
				 
				 request.setAttribute("turnosConsulta", turnos1);
					
					getServletContext().getRequestDispatcher("/consulta_turno.jsp").forward(request,response);
			 } else {
				 request.setAttribute("erro", "sem valores");
					
					getServletContext().getRequestDispatcher("/consulta_turno.jsp").forward(request,response);
			 }
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
